

/******************************************************************************
* Copyright (C) 2021, BBI Technology Co.,Ltd All rights reserved.
*
* Author: Embedded software group
*
* file： 
*
*
*Note：此文件位于对象层
 ******************************************************************************/
 
#include "app_cjson.h"   

void APP_cjson_parseArray(char * pJson);

report_stringbuff_Struct  report_stringbuff={

 .monvol_dayvol_eventlog_buff="11",

};
report_Data_Struct   report_Data_buf[]=
{
   { "msgid" , NULL , 1 , cJSON_Number},   //消息ID,代表消息类型
   { "seq"  , NULL , 1 , cJSON_Number},     //报文的序列号
   { "num"   , report_stringbuff.num_buff  , NULL,cJSON_String},
   { "imei",   report_stringbuff.imei_buff  , NULL,cJSON_String},
   { "iccid",  report_stringbuff.iccid_buff , NULL,cJSON_String},
   { "dev_status",NULL,1,cJSON_Number},
   { "batvol",NULL,1,cJSON_Number},
   { "report",NULL,1,cJSON_Number},
   { "rsii",NULL,1,cJSON_Number},
   { "dayvol",report_stringbuff.monvol_dayvol_eventlog_buff,NULL,cJSON_String}, 
   { "monvol", report_stringbuff.monvol_dayvol_eventlog_buff , NULL,cJSON_String},
   { "eventlog" , report_stringbuff.monvol_dayvol_eventlog_buff ,NULL,cJSON_String},  
   { "cmd" , NULL ,1,cJSON_Number},
   { "seq" , NULL ,1,cJSON_Number},
   { "hex" , report_stringbuff.hex_dat ,NULL,cJSON_String}, 
   { "md5" , report_stringbuff.md5 ,NULL,cJSON_String}, 
   { "ver" , report_stringbuff.ver ,NULL,cJSON_String}, 
   { "com" , NULL ,1,cJSON_Number},
};




cJSON *  cJSON_createObject()
{
    cJSON * pJsonRoot = NULL; 
	
	pJsonRoot = cJSON_CreateObject();		
	if(NULL == pJsonRoot)
	{
			cJSON_Delete(pJsonRoot);
			return pJsonRoot;
	}

   return  (cJSON *)pJsonRoot;

}


/*

  *函数作用日上报结构体里面的数据，编码成json格式
  *参数	1: pdata 结构体入参， 
		2: json_Data_printOut 输出的json字符串流
*/

void  cJSON_LiveData_encode (void *pdata,char *json_Data_printOut)
{
    cJSON * pJsonRoot = NULL;  
	char  *p= NULL;  
	uint16_t  JsonLength=0;		
    report_Data_Struct   *reportData= pdata;
	
    //unsigned char cmd_cb_cnt = sizeof(report_Data_buf) / sizeof(report_Data_buf[0]);
	
    pJsonRoot = cJSON_createObject();
	
	if(reportData[MSGID].valueint>=4)  //ota编码
	{
		
	   cJSON_AddStringToObject(pJsonRoot, reportData[MSGID].name, reportData[MSGID].valuestring);
	   cJSON_AddStringToObject(pJsonRoot, reportData[COM].name, reportData[COM].valuestring);
	   if(reportData[MSGID].valueint==4)
	   {   
		  cJSON_AddStringToObject(pJsonRoot, reportData[VER].name, reportData[VER].valuestring);
	   }
	}
	else   //
	{
		for(uint8_t i=0; i<9;i++)	
		{
			if( reportData[i].type == cJSON_String)
			{
				cJSON_AddStringToObject(pJsonRoot, reportData[i].name, reportData[i].valuestring); 
			}
			else
			{
				cJSON_AddNumberToObject(pJsonRoot, reportData[i].name, reportData[i].valueint); 
			}
		}
		if(reportData[MSGID].valueint==1)
		{
			cJSON_AddStringToObject(pJsonRoot, reportData[DAYVOL].name, reportData[DAYVOL].valuestring); 
		
		}
		else if(reportData[MSGID].valueint==2)
		{
			cJSON_AddStringToObject(pJsonRoot, reportData[MONVOL].name, reportData[MONVOL].valuestring); 
		
		}
		else if(reportData[MSGID].valueint==3)
		{
			cJSON_AddStringToObject(pJsonRoot, reportData[EVENTLOG].name, reportData[EVENTLOG].valuestring); 
		
		}	
	}
		 
	p = cJSON_Print(pJsonRoot);  	
	JsonLength=strlen(p);

	for(uint8_t i=0; i<JsonLength;i++)	
	{		
		json_Data_printOut[i] = p[i];			
	}
	memset((char *)(report_Data_buf),0,sizeof(report_Data_Struct));
	APP_cjson_parseArray(p);
	cJSON_Delete(pJsonRoot);   
  	
	vPortFree(p);     	
	
}





/*

  *函数作用：json格式解码到，结构体里面的数据，
  *参数	1: pJson json字符串

*/

void APP_cjson_parseArray(char * pJson)
{
    cJSON * root;
	cJSON * pSub;
    int iSize;

	int iCnt;	
	
//	unsigned char cmd_cb_cnt = sizeof(report_Data_buf) / sizeof(report_Data_buf[0]);
	
    if(NULL == pJson)
    {                                                                                                
        return ;
    }
    root = NULL;
    if((root = cJSON_Parse(pJson)) == NULL)
    {
        return ;
    }
    iSize = cJSON_GetArraySize(root);
    for(iCnt = 0; iCnt < iSize; iCnt++)
    {
        pSub = cJSON_GetArrayItem(root, iCnt);
        if(NULL == pSub)
        {
            continue;
        }
        if(strstr(pSub->string,"msgid")!=NULL)		
        {   
	       report_Data_buf[MSGID].valueint = pSub->valueint ;
	    }
		else if(strstr(pSub->string,"cmd")!=NULL)
        {
			report_Data_buf[CMD].valueint = pSub->valueint ;	
		}
		else if(strstr(pSub->string,"seq")!=NULL)
        {
			report_Data_buf[SEQ].valueint = pSub->valueint ;	
		}	

 		else if(strstr(pSub->string,"ver")!=NULL)
        {
			 memcpy(report_Data_buf[VER].valuestring, pSub->valuestring,6);	
		}			
		else if(strstr(pSub->string,"hex")!=NULL)
        {
			 memcpy(report_Data_buf[HEX].valuestring, pSub->valuestring,512);	
		}		
	    else if(strstr(pSub->string,"md5")!=NULL)
        {
			 memcpy(report_Data_buf[MD5].valuestring, pSub->valuestring,32);	
	 	}
		
    }
    
    return;
}




